import 'package:dk_flutter_base/dk_ui.dart';
import 'package:dk_flutter_module_sale/common/component/dk_toast_widget.dart';
import 'package:flutter/cupertino.dart';

class DKLeftDrawerSelectTimeWidget extends StatefulWidget {
  final DateTime startDateTime;
  final DateTime endDateTime;
  final Function(DateTime, DateTime) onDaySelectPressed;
  DKLeftDrawerSelectTimeWidget({this.startDateTime, this.endDateTime, this.onDaySelectPressed});

  @override
  TimeSelectDrawerState createState() => TimeSelectDrawerState(
      this.onDaySelectPressed, this.startDateTime, this.endDateTime);
}

class TimeSelectDrawerState extends State<DKLeftDrawerSelectTimeWidget> {
  String startTimeStr;
  String endTimeStr;
  Function(DateTime, DateTime) onDaySelectPressed;
  //日期选择
  DkPicker typePicker1;
  DkPicker typePicker2;

  DateTime startDateTime;
  DateTime endDateTime;

  TimeSelectDrawerState(
      this.onDaySelectPressed, this.startDateTime, this.endDateTime);

  @override
  void initState() {
    super.initState();
    startTimeStr = (startDateTime != null)
        ? startDateTime.toString().substring(0, 10)
        : "选择开始时间";
    endTimeStr = (endDateTime != null)
        ? endDateTime.toString().substring(0, 10)
        : "选择结束时间";

    startDateTime = startDateTime != null ? startDateTime : DateTime.now();
    endDateTime = endDateTime != null ? endDateTime : DateTime.now();
  }

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Stack(
        children: <Widget>[
          Container(
            color: Colors.white,
            padding: EdgeInsets.symmetric(horizontal: 16),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.start,
              children: <Widget>[
                SizedBox(
                  height: 30,
                ),
                Row(
                  children: <Widget>[
                    Text(
                      "预约带看时间",
                      textAlign: TextAlign.left,
                      style: TextStyle(
                        color: Color(0xff666666),
                        fontSize: 15,
                      ),
                    ),
                    Spacer(),
                  ],
                ),
                SizedBox(
                  height: 20,
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.start,
                  children: <Widget>[
                    Expanded(
                      child: GestureDetector(
                        child: Container(
                          height: 32,
                          child: Center(
                            child: Text(
                              startTimeStr,
                              style: TextStyle(
                                  color: Color(0xff999999), fontSize: 12),
                            ),
                          ),
                          decoration: BoxDecoration(
                              color: Colors.white,
                              border: Border.all(
                                  color: Color(0xffebebeb), width: 1),
                              borderRadius:
                                  BorderRadius.all(Radius.circular(32))),
                        ),
                        onTap: () {
                          showMothPicker1(startDateTime);
                        },
                      ),
                    ),
                    SizedBox(
                      width: 15,
                    ),
                    Center(
                      child: Container(
                        height: 0.5,
                        width: 20,
                        color: Color(0xffebebeb),
                      ),
                    ),
                    SizedBox(
                      width: 15,
                    ),
                    Expanded(
                      child: GestureDetector(
                        child: Container(
                          height: 32,
                          child: Center(
                            child: Text(
                              endTimeStr,
                              style: TextStyle(
                                  color: Color(0xff999999), fontSize: 12),
                            ),
                          ),
                          decoration: BoxDecoration(
                              color: Colors.white,
                              border: Border.all(
                                  color: Color(0xffebebeb), width: 1),
                              borderRadius:
                                  BorderRadius.all(Radius.circular(32))),
                        ),
                        onTap: () {
                          showMothPicker2(endDateTime);
                        },
                      ),
                    ),
                  ],
                ),
              ],
            ),
          ),
          Positioned(
            bottom: 0,
            left: 0,
            right: 0,
            child: //底部选择按钮
                Container(
              height: 49,
              child: Row(
                children: <Widget>[
                  GestureDetector(
                    child: Container(
                      width: 95,
                      height: 49,
                      decoration: BoxDecoration(
                        color: Color(0xffF7F8F9),
                        boxShadow: [
                          BoxShadow(
                              color: Color.fromRGBO(0, 0, 0, 0.16),
                              offset: Offset(0, 1.0),
                              blurRadius: 2.0,
                              spreadRadius: 0.0)
                        ],
                      ),
                      child: Center(
                        child: Text(
                          "重置",
                          maxLines: 1,
                          style:
                              TextStyle(color: Color(0xff666666), fontSize: 16),
                        ),
                      ),
                    ),
                    onTap: () {
                      setState(() {
                        startTimeStr = "选择开始时间";
                        endTimeStr = "选择结束时间";
                        startDateTime = null;
                        endDateTime = null;
                      });
                    },
                  ),
                  Expanded(
                    child: GestureDetector(
                      child: Container(
                        height: 49,
                        decoration: new BoxDecoration(
                          gradient: const LinearGradient(
                              colors: [Color(0xff3FC0BA), Color(0xff3CBBC5)]),
                        ),
                        child: Center(
                          child: Text("确定",
                              style:
                                  TextStyle(fontSize: 16, color: Colors.white)),
                        ),
                      ),
                      onTap: () {
                        if (this.startTimeStr == "选择开始时间" &&
                            this.endTimeStr != "选择结束时间") {
                          DKToastWidget.ShowToast("请选择开始时间");
                          return;
                        } else if (this.startTimeStr != "选择开始时间" &&
                            this.endTimeStr == "选择结束时间") {
                          DKToastWidget.ShowToast("请选择结束时间");
                          return;
                        }
                        if (onDaySelectPressed != null) {
                          if (this.startTimeStr == "选择开始时间" &&
                              this.endTimeStr == "选择结束时间") {
                            onDaySelectPressed(null, null);
                          } else {
                            if (startDateTime.isAfter(endDateTime)) {
                              onDaySelectPressed(endDateTime, startDateTime);
                            } else {
                              onDaySelectPressed(startDateTime, endDateTime);
                            }
                          }
                        }
                      },
                    ),
                  ),
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }

  void showMothPicker1(DateTime curDateTime) {
    if (null == typePicker1) {
      typePicker1 = DkPicker(
        title: Text(
          "选择时间",
          style: TextStyle(fontSize: 14, color: Color(0xff999999)),
        ),
        confirmText: "完成",
        cancelTextStyle: TextStyle(
            color: Color(0xff666666),
            fontSize: 14,
            fontWeight: FontWeight.normal),
        confirmTextStyle: TextStyle(
            color: Color(0xff3DBCC6),
            fontSize: 14,
            fontWeight: FontWeight.normal),
        adapter: new DateTimePickerAdapter(
          type: PickerDateTimeType.kYMD,
          isNumberMonth: true,
          yearSuffix: "年",
          monthSuffix: "月",
          daySuffix: "日",
          yearBegin: 2000,
          maxValue: DateTime.now().add(Duration(days: 365 * 20)),
          value: curDateTime,
        ),
        onConfirm: (DkPicker picker, List value) {
          curDateTime = DateTime(2000 + value[0], value[1] + 1, value[2] + 1);
          setState(() {
            startTimeStr = curDateTime.toString().substring(0, 10);
            startDateTime = curDateTime;
          });
        },
      );
    }

    typePicker1.showModal(context);
  }

  void showMothPicker2(DateTime curDateTime) {
    if (null == typePicker2) {
      typePicker2 = DkPicker(
        title: Text(
          "选择时间",
          style: TextStyle(fontSize: 14, color: Color(0xff999999)),
        ),
        confirmText: "完成",
        cancelTextStyle: TextStyle(
            color: Color(0xff666666),
            fontSize: 14,
            fontWeight: FontWeight.normal),
        confirmTextStyle: TextStyle(
            color: Color(0xff3DBCC6),
            fontSize: 14,
            fontWeight: FontWeight.normal),
        adapter: new DateTimePickerAdapter(
          type: PickerDateTimeType.kYMD,
          isNumberMonth: true,
          yearSuffix: "年",
          monthSuffix: "月",
          daySuffix: "日",
          yearBegin: 2000,
          maxValue: DateTime.now().add(Duration(days: 365 * 20)),
          value: curDateTime,
        ),
        onConfirm: (DkPicker picker, List value) {
          curDateTime = DateTime(2000 + value[0], value[1] + 1, value[2] + 1);
          setState(() {
            endTimeStr = curDateTime.toString().substring(0, 10);
            endDateTime = curDateTime;
          });
        },
      );
    }
    typePicker2.showModal(context);
  }
}
